﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Threading;

namespace zmMoniter
{
    class OpenDeck
    {
        private mainForm main = null;
        private int iDeckID = -1;
        public OpenDeck(mainForm frm, int DeckID)
        {
            main = frm;
            iDeckID = DeckID;
        }
        public  void open()
        {
            if (iDeckID == -1)
            {
                main.pgsPanel.SetBounds(0, 0, 1, 1);
                return;
            }
            System.Threading.Thread th = null;
            th = new Thread(new ThreadStart(this.DoWork));
            
            th.IsBackground = true;
            th.Start();
            
        }
        public void DoWork()
        {
            main.pgsPanel.Description = "更新振捣设备数据...";
            //////////////////////////////lg-add//////////////////////////////////////
            PublicDataCls db = new PublicDataCls();
            db.OpenConn();
            string planDevice = main.getPlanDevice(iDeckID);
            ArrayList al = new ArrayList();
            string sSQL = "";
            string upDatePlanDevices = "";
            if (planDevice != "")
            {
                sSQL = @"SELECT * FROM 振捣设备基本信息表 WHERE (编号 IN (" + planDevice + "))";
                DataTable deviceDT = db.ReturnDataTable(sSQL);
                if (deviceDT != null)//modify by lance 2013-1-20
                {
                    string[] planDeviceList = planDevice.Split(',');
                    for (int i = 0; i < planDeviceList.Length; i++)
                    {
                        string sID = planDeviceList[i];//设备编号
                        for (int j = 0; j < deviceDT.Rows.Count; j++)
                        {
                            if (deviceDT.Rows[j]["编号"].ToString() == sID)
                            {
                                if (deviceDT.Rows[j]["使用设备状态"].ToString() == "1")//在线
                                {
                                    MessageBox.Show(sID + "号设备正在使用,请调度其它设备!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                                }
                                else
                                {
                                    sSQL = "UPDATE 振捣设备基本信息表 SET 仓面号=" + iDeckID + " WHERE 编号=" + sID;//设备上线标记
                                    al.Add(sSQL);
                                    if (upDatePlanDevices == "")
                                    {
                                        upDatePlanDevices += sID;
                                    }
                                    else
                                    {
                                        upDatePlanDevices += "," + sID;
                                    }
                                }
                                break;
                            }
                        }
                    }
                }
                
            }
            sSQL = "UPDATE 仓面信息 set 规划设备='" + upDatePlanDevices + "' where 编号=" + iDeckID;
            al.Add(sSQL);
            for (int i = 0; i < al.Count; i++)
            {
                int x = db.ExecuteSQL((string)al[i]);
            }
            //////////////////////////////lg-add//////////////////////////////////////

            main.pgsPanel.Description = "将开仓状态写入数据库...";
            DateTime now = System.DateTime.Now;
            sSQL = "UPDATE 仓面信息 SET 开仓状态=1,开仓时间=to_date('" + now.ToString("yyyy/MM/dd hh:mm:ss") + "','YYYY/MM/DD HH24:MI:SS')" + " WHERE 编号=" + iDeckID;
            
            int iResult = db.ExecuteSQL(sSQL);
            if (iResult > 0)
            {
                main.pgsPanel.Description = "通知监控服务端...";
                main.seedMessage("01;" + iDeckID);
                main.setDeckState(iDeckID, 1);
                main.pgsPanel.Description = "通知监控服务端成功！";
                // MessageBox.Show("开仓成功！", "开仓提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else
            {
                main.pgsPanel.Description = "通知监控服务端失败！";
                //MessageBox.Show("开仓失败！", "开仓提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            db.CloseConn();
            main.pgsPanel.Description = "更新图形！";
            main.upDateGraph();
            main.pgsPanel.SetBounds(0, 0, 1, 1);
        }
    }
}
